package com.zk.config;

import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
import org.springframework.web.filter.CharacterEncodingFilter;
import org.springframework.web.servlet.support.AbstractDispatcherServletInitializer;

import javax.servlet.Filter;
import javax.swing.*;

public class ServletContainersInitConfig extends AbstractDispatcherServletInitializer {
    // 加载springmvc配置类。创建Servlet容器时，加载SpringMVC对应
    //的bean并放入WebApplicationContext对象范围中，而WebApplicationContext的作用
    //范围为ServletContext范围，即整个web容器范围
    protected WebApplicationContext createServletApplicationContext() {
        //初始化 webApplicationContext 对象
        AnnotationConfigWebApplicationContext webApplicationContext = new AnnotationConfigWebApplicationContext();
        // 加载指定配置类
        webApplicationContext.register(SpringMvcConfig.class);
        return webApplicationContext;
    }

    // 设置由springmvc控制器处理的请求映射路径，即SpringMVC拦截哪些请求
    protected String[] getServletMappings() {
        return new String[]{"/"};
    }

    //加载spring配置类。如果创建Servlet容器时需要加载非SpringMVC
    //对应的bean,使用当前方法进行，使用方式和createServletApplicationContext相同。
    //createServletApplicationContext用来加载SpringMVC环境
    //createRootApplicationContext用来加载Spring环境
    protected WebApplicationContext createRootApplicationContext() {
        return null;
    }

    @Override
    protected Filter[] getServletFilters() {
        CharacterEncodingFilter filter = new CharacterEncodingFilter();
        filter.setEncoding("UTF-8");
        return new Filter[]{filter};
    }
}
